package com.simpletour.qa.service.util;

import com.simpletour.qa.common.entity.User;
import com.simpletour.qa.service.entity.BaseUser;

public class UserUtil {

    private static final ThreadLocal<Integer> INTEGER_THREAD_LOCAL = new ThreadLocal<>();

    private static final int DEFAULT_USER_ID = -1;

    public static Integer currentUserId() {
        int userId = DEFAULT_USER_ID;
        if(INTEGER_THREAD_LOCAL.get() != null){
            userId = INTEGER_THREAD_LOCAL.get();
        }
        return userId;
    }

    public static void setCurrentUserId(Integer currentUserId) {
        INTEGER_THREAD_LOCAL.set(currentUserId);
    }

    public static void removeCurrentUserId(){
        INTEGER_THREAD_LOCAL.remove();
    }


    public static BaseUser convertUser(User user) {
        BaseUser baseUser = new BaseUser();
        baseUser.setUserId(user.getUserId());
        baseUser.setPosition(user.getPosition());
        baseUser.setUsername(user.getUsername());
        baseUser.setGender(user.getGender());
        baseUser.setAvatar(user.getAvatar());
        return baseUser;
    }
}
